import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import java.awt.BorderLayout;

import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class PomsManager {

	private JFrame frame;
	public JFrame getFrame() {
		return frame;
	}

	public void setFrame(JFrame frame) {
		this.frame = frame;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PomsManager window = new PomsManager();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	/**
	 * Create the application.
	 */
	public PomsManager() {
		
 
		
		initialize();
	}
	/**
	 * Initialize the contents of the frame.
	 */
	JTextArea textArea;
	JTextArea textArea_1;
	JTextArea textArea_2;
	JTextArea textArea_3;
	JTextArea textArea_4;
	JTextArea textArea_5;
	JTextArea textArea_6;
	
	JTextArea textArea_7;
	JTextArea textArea_8;
	JTextArea textArea_9;
	JTextArea textArea_10;
	JTextArea textArea_11;
	JTextArea textArea_12;
	
	JTextArea textArea_13;
	
	
	JTabbedPane tabbedPane_1; 
	JTabbedPane tabbedPane; 
	private void initialize() {
		frame = new JFrame();
		frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
		
		JButton btnNewButton = new JButton("Save");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DeployManger d = new DeployManger();
				String titolo = "";
				
				titolo = tabbedPane.getTitleAt(tabbedPane.getSelectedIndex());
				if( titolo.equals("FlowsCommon"))
				{
					scriviFile(textArea_6.getText(),d.getPathWorkspace()+"\\FlowsCommon\\pom.xml");
				}
				titolo = tabbedPane.getTitleAt(tabbedPane.getSelectedIndex());
				if( titolo.equals("FlowsSchedulerWeb"))
				{
					scriviFile(textArea_13.getText(),d.getPathWorkspace()+"\\FlowsSchedulerWeb\\pom.xml");
				}
				if( titolo.equals("BatchApp"))
				{
					scriviFile(textArea_1.getText(),d.getPathWorkspace()+"\\BatchApp\\BatchEngine\\pom.xml");
					
					scriviFile(textArea_2.getText(),d.getPathWorkspace()+"\\BatchApp\\BatchAppWar\\pom.xml");
					scriviFile(textArea_3.getText(),d.getPathWorkspace()+"\\BatchApp\\BatchAppEar\\pom.xml");
					scriviFile(textArea_4.getText(),d.getPathWorkspace()+"\\BatchApp\\RgiService\\pom.xml");
					scriviFile(textArea_5.getText(),d.getPathWorkspace()+"\\BatchApp\\BatchWrap\\pom.xml");
				}
				if( titolo.equals("FlowsOperation"))
				{
					scriviFile(textArea_12.getText(),d.getPathWorkspace()+"\\FlowsOperation\\pom.xml");
					scriviFile(textArea_7.getText(),d.getPathWorkspace()+"\\FlowsOperation\\FlowsAnia\\pom.xml");
					scriviFile(textArea_8.getText(),d.getPathWorkspace()+"\\FlowsOperation\\FlowsEngine\\pom.xml");
					scriviFile(textArea_9.getText(),d.getPathWorkspace()+"\\FlowsOperation\\FlowsLogParser\\pom.xml");
					scriviFile(textArea_10.getText(),d.getPathWorkspace()+"\\FlowsOperation\\FlowsOperationWar\\pom.xml");
					scriviFile(textArea_11.getText(),d.getPathWorkspace()+"\\FlowsOperation\\FlowsOperationEar\\pom.xml");
				}
				
				JOptionPane.showMessageDialog(null, "Modifica effettuata");
			}
		});
		frame.getContentPane().add(btnNewButton, BorderLayout.SOUTH);
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
		
		tabbedPane_1 = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.addTab("BatchApp", null, tabbedPane_1, null);
		
		JScrollPane scrollPane = new JScrollPane();
		tabbedPane_1.addTab("BatchApp", null, scrollPane, null);
		textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		scrollPane.getHorizontalScrollBar().setValue(0);
		scrollPane.getVerticalScrollBar().setValue(0);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		
		tabbedPane_1.addTab("BatchEngine", null, scrollPane_1, null);
		
		
		textArea_1 = new JTextArea();
		scrollPane_1.setViewportView(textArea_1);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		tabbedPane_1.addTab("BatchAppWar", null, scrollPane_2, null);
		
		textArea_2 = new JTextArea();
		scrollPane_2.setViewportView(textArea_2);
		
		JScrollPane scrollPane_3 = new JScrollPane();
		tabbedPane_1.addTab("BatchAppEar", null, scrollPane_3, null);
		
		textArea_3 = new JTextArea();
		scrollPane_3.setViewportView(textArea_3);
		
		JScrollPane scrollPane_4 = new JScrollPane();
		tabbedPane_1.addTab("RgiService", null, scrollPane_4, null);
		
		textArea_4 = new JTextArea();
		scrollPane_4.setViewportView(textArea_4);
		
		JScrollPane scrollPane_5 = new JScrollPane();
		tabbedPane_1.addTab("BatchWrap", null, scrollPane_5, null);
		
		textArea_5 = new JTextArea();
		scrollPane_5.setViewportView(textArea_5);
		
		JScrollPane scrollPane_6 = new JScrollPane();
		tabbedPane.addTab("FlowsCommon", null, scrollPane_6, null);
		
		textArea_6 = new JTextArea();
		scrollPane_6.setViewportView(textArea_6);
		
		JTabbedPane tabbedPane_2 = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.addTab("FlowsOperation", null, tabbedPane_2, null);
		
		JScrollPane scrollPane_7 = new JScrollPane();
		tabbedPane_2.addTab("FlowsOperation", null, scrollPane_7, null);
		
		textArea_12 = new JTextArea();
		scrollPane_7.setViewportView(textArea_12);
		
		JScrollPane scrollPane_8 = new JScrollPane();
		tabbedPane_2.addTab("FlowsAnia", null, scrollPane_8, null);
		
		textArea_7 = new JTextArea();
		scrollPane_8.setViewportView(textArea_7);
		
		JScrollPane scrollPane_9 = new JScrollPane();
		tabbedPane_2.addTab("FlowsEngine", null, scrollPane_9, null);
		
		textArea_8 = new JTextArea();
		scrollPane_9.setViewportView(textArea_8);
		
		JScrollPane scrollPane_10 = new JScrollPane();
		tabbedPane_2.addTab("FlowsLogParser", null, scrollPane_10, null);
		
		textArea_9 = new JTextArea();
		scrollPane_10.setViewportView(textArea_9);
		
		JScrollPane scrollPane_11 = new JScrollPane();
		tabbedPane_2.addTab("FlowsOperationWar", null, scrollPane_11, null);
		
		textArea_10 = new JTextArea();
		scrollPane_11.setViewportView(textArea_10);
		
		JScrollPane scrollPane_12 = new JScrollPane();
		tabbedPane_2.addTab("FlowsOperationEar", null, scrollPane_12, null);
		
		textArea_11 = new JTextArea();
		scrollPane_12.setViewportView(textArea_11);
		
		JScrollPane scrollPane_13 = new JScrollPane();
		tabbedPane.addTab("FlowsSchedulerWeb", null, scrollPane_13, null);
		
		textArea_13 = new JTextArea();
		scrollPane_13.setViewportView(textArea_13);
		settaPomEsistenti();
	}

	private void settaPomEsistenti() {
		BufferedReader br = null;
		String messaggio = "";
		try {
			
			String sCurrentLine;
			DeployManger d = new DeployManger();
			br = new BufferedReader(new InputStreamReader(new FileInputStream(d.getPathWorkspace()+"\\BatchApp\\pom.xml"),"UTF-8"));
			
			while ((sCurrentLine = br.readLine()) != null) {
				messaggio = messaggio+sCurrentLine+"\n";
			}
			textArea.setText(messaggio);
			textArea.setCaretPosition(0);
			br.close();
			sCurrentLine = "";;
			messaggio = "";
			br = new BufferedReader(new InputStreamReader(new FileInputStream(d.getPathWorkspace()+"\\BatchApp\\BatchEngine\\pom.xml"),"UTF-8"));
			
			while ((sCurrentLine = br.readLine()) != null) {
				messaggio = messaggio+sCurrentLine+"\n";
			}
			textArea_1.setText(messaggio);
			textArea_1.setCaretPosition(0);
			br.close();
			
			sCurrentLine = "";;
			messaggio = "";
			br = new BufferedReader(new InputStreamReader(new FileInputStream(d.getPathWorkspace()+"\\BatchApp\\BatchAppWar\\pom.xml"),"UTF-8"));
			
			while ((sCurrentLine = br.readLine()) != null) {
				messaggio = messaggio+sCurrentLine+"\n";
			}
			textArea_2.setText(messaggio);
			textArea_2.setCaretPosition(0);
			br.close();
			
			sCurrentLine = "";;
			messaggio = "";
			br = new BufferedReader(new InputStreamReader(new FileInputStream(d.getPathWorkspace()+"\\BatchApp\\BatchAppEar\\pom.xml"),"UTF-8"));
			
			while ((sCurrentLine = br.readLine()) != null) {
				messaggio = messaggio+sCurrentLine+"\n";
			}
			textArea_3.setText(messaggio);
			textArea_3.setCaretPosition(0);
			br.close();
			
			sCurrentLine = "";;
			messaggio = "";
			br = new BufferedReader(new InputStreamReader(new FileInputStream(d.getPathWorkspace()+"\\BatchApp\\RgiService\\pom.xml"),"UTF-8"));
			
			while ((sCurrentLine = br.readLine()) != null) {
				messaggio = messaggio+sCurrentLine+"\n";
			}
			textArea_4.setText(messaggio);
			textArea_4.setCaretPosition(0);
			br.close();
			
			sCurrentLine = "";;
			messaggio = "";
			br = new BufferedReader(new InputStreamReader(new FileInputStream(d.getPathWorkspace()+"\\BatchApp\\BatchWrap\\pom.xml"),"UTF-8"));
			
			while ((sCurrentLine = br.readLine()) != null) {
				messaggio = messaggio+sCurrentLine+"\n";
			}
			textArea_5.setText(messaggio);
			textArea_5.setCaretPosition(0);
			br.close();
			
			sCurrentLine = "";;
			messaggio = "";
			br = new BufferedReader(new InputStreamReader(new FileInputStream(d.getPathWorkspace()+"\\FlowsCommon\\pom.xml"),"UTF-8"));
			
			while ((sCurrentLine = br.readLine()) != null) {
				messaggio = messaggio+sCurrentLine+"\n";
			}
			textArea_6.setText(messaggio);
			textArea_6.setCaretPosition(0);
			br.close();
			
			sCurrentLine = "";;
			messaggio = "";
			br = new BufferedReader(new InputStreamReader(new FileInputStream(d.getPathWorkspace()+"\\FlowsOperation\\pom.xml"),"UTF-8"));
			
			while ((sCurrentLine = br.readLine()) != null) {
				messaggio = messaggio+sCurrentLine+"\n";
			}
			textArea_7.setText(messaggio);
			textArea_7.setCaretPosition(0);
			br.close();
			
			sCurrentLine = "";;
			messaggio = "";
			br = new BufferedReader(new InputStreamReader(new FileInputStream(d.getPathWorkspace()+"\\FlowsOperation\\FlowsAnia\\pom.xml"),"UTF-8"));
			
			while ((sCurrentLine = br.readLine()) != null) {
				messaggio = messaggio+sCurrentLine+"\n";
			}
			textArea_7.setText(messaggio);
			textArea_7.setCaretPosition(0);
			br.close();
			
			sCurrentLine = "";;
			messaggio = "";
			br = new BufferedReader(new InputStreamReader(new FileInputStream(d.getPathWorkspace()+"\\FlowsOperation\\FlowsEngine\\pom.xml"),"UTF-8"));
			
			while ((sCurrentLine = br.readLine()) != null) {
				messaggio = messaggio+sCurrentLine+"\n";
			}
			textArea_8.setText(messaggio);
			textArea_8.setCaretPosition(0);
			br.close();
			
			sCurrentLine = "";;
			messaggio = "";
			br = new BufferedReader(new InputStreamReader(new FileInputStream(d.getPathWorkspace()+"\\FlowsOperation\\FlowsLogParser\\pom.xml"),"UTF-8"));
			
			while ((sCurrentLine = br.readLine()) != null) {
				messaggio = messaggio+sCurrentLine+"\n";
			}
			textArea_9.setText(messaggio);
			textArea_9.setCaretPosition(0);
			br.close();
			
			sCurrentLine = "";;
			messaggio = "";
			br = new BufferedReader(new InputStreamReader(new FileInputStream(d.getPathWorkspace()+"\\FlowsOperation\\FlowsOperationWar\\pom.xml"),"UTF-8"));
			
			while ((sCurrentLine = br.readLine()) != null) {
				messaggio = messaggio+sCurrentLine+"\n";
			}
			textArea_10.setText(messaggio);
			textArea_10.setCaretPosition(0);
			br.close();
			
			sCurrentLine = "";;
			messaggio = "";
			br = new BufferedReader(new InputStreamReader(new FileInputStream(d.getPathWorkspace()+"\\FlowsOperation\\FlowsOperationEar\\pom.xml"),"UTF-8"));
			
			while ((sCurrentLine = br.readLine()) != null) {
				messaggio = messaggio+sCurrentLine+"\n";
			}
			textArea_11.setText(messaggio);
			textArea_11.setCaretPosition(0);
			br.close();
			
			sCurrentLine = "";;
			messaggio = "";
			br = new BufferedReader(new InputStreamReader(new FileInputStream(d.getPathWorkspace()+"\\FlowsOperation\\pom.xml"),"UTF-8"));
			
			while ((sCurrentLine = br.readLine()) != null) {
				messaggio = messaggio+sCurrentLine+"\n";
			}
			textArea_12.setText(messaggio);
			textArea_12.setCaretPosition(0);
			br.close();
			

			sCurrentLine = "";;
			messaggio = "";
			br = new BufferedReader(new InputStreamReader(new FileInputStream(d.getPathWorkspace()+"\\FlowsSchedulerWeb\\pom.xml"),"UTF-8"));
			
			while ((sCurrentLine = br.readLine()) != null) {
				messaggio = messaggio+sCurrentLine+"\n";
			}
			textArea_13.setText(messaggio);
			textArea_13.setCaretPosition(0);
			br.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null)br.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		
	}
	public void scriviFile(String content, String path)
	{

		try {
			File file = new File(path);
			FileWriter fw = new FileWriter(file.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(content);
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}



	}
}
